<?php echo $this->headLink()->appendStylesheet($this->Css('jtip.css'))?>
<?php echo $this->headScript()
	->appendFile($this->js('plugins/jquery.form.js'))
	//->appendFile($this->js('plugins/jquery.block.js'))
	->appendFile($this->js('plugins/jtip.js'))
	->captureStart();
?>

function Subject() {
    var yearSemester;
    // 選擇班級
    var classSn ;
    // 修改模式
    var editMode ;

    return {
    // 設定科目
     setSubject:function() {
        $.get('<?php echo $this->route('get-subject')?>',
        {year_semester:this.yearSemester, classSn: this.classSn, editMode:this.editMode},
        function(data){
            $("#settable").html(data);
        });
        $.get('<?php echo $this->route('yet-set')?>',
          {year_semester:this.yearSemester, classSn: this.classSn, editMode:this.editMode},
        function(data){
            $("#addtable").html(data);
        });
      },
      //初始化
      init: function(){
        this.yearSemester = $("#year_semester").val();
        this.classSn = $("#class_sn").val();
        this.editMode = 0;
        this.setSubject();
      },
      // 設定年級或班級
      setClassSn: function(classSn) {
        this.classSn = classSn;
      },
      // 取得目前的年級或班級
      getClassSn: function() {
        return this.classSn;
      },
      // 設定修改模式
      setEditMode: function(e){
        if (e ==0)
        $("#cancelBtn").attr('disabled','disabled');
        else
        $("#cancelBtn").attr('disabled','');
        this.editMode = e;
      },
      // 取修改模式
      getEditMode: function(){
        return this.editMode ;
      },
      // 刪除科目
      deleteSubject:function(sn){
         var s = this;
        $.getJSON('<?php echo $this->route('deletesubject')?>',{sn:sn},function(data){
            if (!data)
                alert('<?php echo $this->translate('There Course Scheduling information can not be deleted')?>');
             else
                s.setSubject();
        });
      },
      // 加權同節數
      setRateSameSections:function(){
        var s = this;
         $.get('<?php echo $this->route('rate2sections')?>',
        {year_semester:this.yearSemester, classSn: this.classSn},
        function(data){
            if (data)
                s.setSubject();
        });

      },
      // 新增一科目
      addOneSubject:function(sn){
        var s = this;
        var sameChk = 0;
        if ($('#sameChkBox').attr('checked'))
             sameChk = 1;
        $.getJSON('<?php echo $this->route('setonesubject')?>',
          {year_semester:this.yearSemester, classSn: this.classSn, sn:sn, sameChk:sameChk},
        function(data){
          if (data)
            s.setSubject();
        });
      },
    };
}

var subject = new Subject();

$(document).ready(function(){
    subject.init();

    // 改變年級班級
    $("#class_sn").change(function(){
        subject.setClassSn($(this).val());
        subject.setSubject();
    });

     // 改變學期
    $("#year_semester").change(function(){
        $("#myform").submit();
    });

   // 修改模式
   $("#editBtn").click(function(){
       $(this).attr('value','<?php echo $this->translate('save')?>');
        // 確定修改
        if (subject.getEditMode() == 1) {
            if ($('#sameChkBox').attr('checked'))
                $("#sameWholeGrade").attr('value',1);
             else
                $("#sameWholeGrade").attr('value',0);
            $("#editForm").submit();
        }
        else {
            subject.setEditMode(1);
            subject.setSubject();
        }
   });

    // 放棄修改
    $("#cancelBtn").click(function(){

       $("#editBtn").attr('value','<?php echo $this->translate('edit')?>');
        subject.setEditMode(0);
        subject.setSubject();
   });


    // 刪除 event
   $("#settable .deleteBtn").live('click',function(){
        var id = $(this).attr('id').substr(4);
        subject.deleteSubject(id);
   });

   // 加權同節數
   $("#rate_same_sections").click(function(){
        subject.setRateSameSections();
   });

   // 加入一科
   $("#addtable li a").live('click',function(){
        var id = $(this).attr('id').substr(2);
        subject.addOneSubject(id);
   });

 });


<?php $this->headScript()->captureEnd()?>
<?php echo $this->headStyle()->captureStart() ?>
	#settable {float:left;width:80%;margin:5px}
	#settable table {width:100%}
	#addlist table {width:85%;background:#ccc;margin:5px auto }
	#addlist tr {background:#fff;padding:2px}
	#addlist td,#settable td {padding:3px;text-align:center}
	#right_message{padding:1px;float:left;width:150px;margin:3px}
	#settable {background:#ccc}
	#settable tr{background:#fff}
	#settable img {border:0px}
	#addtable {background:#efe;}
	#addtable h1 {font-size:120%;text-align:center; border:3px solid #0de; background:#efd}
	#addtable li {list-style-type:decimal}
	#addtable a {background:#ff0;color:#00f}
	#addtable hover {color:#fe0;text-decoration:underline;}
	.tol_sec,#total_sections{margin:5px 3px;padding:2px; border:1px solid #dde; background:#ffb}
	#list_all_subject{margin:5px auto;padding:2px;width:100px}
   #rate_same_sections {text-align:right}
<?php echo $this->headStyle()->captureEnd() ?>


<div class="main_body">

<table width="85%" >
<tr>
<td>
<form action="" method="post" id="myform">
<select name="year_semester" class="refresh" id="year_semester">
<?php echo  $this->YearSemesterOptions($this->year_semester)?>
</select>
<select name="class_sn" id="class_sn" >
<?php echo $this->ClassNameOptions($this->year_semester)?>
</select>
 <input id="sameChkBox" type="checkbox"  name="sameWholeGrade" value="1" />
 <label for="sameChkBox" ><?php echo $this->translate('same configure as whole grade')?></label>
</form>
 </td>
 <td>
 <input type="button" id="editBtn" value="<?php echo $this->translate('edit')?>" />
 <input type="button" id="cancelBtn" value="<?php echo $this->translate('cancel')?>"  disabled="disabled"/>
 </td>
 <td>
<span id="rate_same_sections"  class="like_button"  ><a href="#"><?php echo $this->translate('rate same as sections')?></a></span>
</td>
</tr>
</table>

<div id="settable" >

</div>
<div id="addlist" style="display:none"></div>


<div id="right_message">

<div id="addtable"  >

</div>
</div>
</div>
<div style="clear:both"></div>